package com.shanks.home.module.image;

import java.util.Timer;
import java.util.TimerTask;

import com.shanks.home.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

/**
 * background move
 * @author Shanks
 * @time 2014.05.28
 */
public class BgMoveActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		this.setContentView(new MatrixView(this));
	}
	
	private class MatrixView extends View {

		int BACK_HEIGHT = 800;
		Bitmap background;
		Bitmap model;
		int WIDTH = 848;
		int HEIGHT = 480;
		int startY = BACK_HEIGHT - HEIGHT; 
		
		public MatrixView(Context context) {
			super(context);
			background = BitmapFactory.decodeResource(context.getResources(), R.drawable.main_background);
			model = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_model); 
			final Handler handler = new Handler() {
				public void handleMessage(android.os.Message msg) {
					if (msg.what == 0x123) {
						if (startY < 0) {
							startY = BACK_HEIGHT - HEIGHT;
						} else {
							startY -= 3;
						}
					}
					invalidate();
				}
			};
			
			new Timer().schedule(new TimerTask() {
				
				@Override
				public void run() {
					handler.sendEmptyMessage(0x123);
				}
			}, 0, 100);
		}
		
		@Override
		protected void onDraw(Canvas canvas) {
//			super.onDraw(canvas);
			Bitmap bitmap2 = Bitmap.createBitmap(background, 0, startY, WIDTH, HEIGHT);
			canvas.drawBitmap(bitmap2, 0, 0, null);
			canvas.drawBitmap(model, 0, 0, null);
		}
	}
}